/*
#          .,:,,,                                        .::,,,::.          
#        .::::,,;;,                                  .,;;:,,....:i:         
#        :i,.::::,;i:.      ....,,:::::::::,....   .;i:,.  ......;i.        
#        :;..:::;::::i;,,:::;:,,,,,,,,,,..,.,,:::iri:. .,:irsr:,.;i.        
#        ;;..,::::;;;;ri,,,.                    ..,,:;s1s1ssrr;,.;r,        
#        :;. ,::;ii;:,     . ...................     .;iirri;;;,,;i,        
#        ,i. .;ri:.   ... ............................  .,,:;:,,,;i:        
#        :s,.;r:... ....................................... .::;::s;        
#        ,1r::. .............,,,.,,:,,........................,;iir;        
#        ,s;...........     ..::.,;:,,.          ...............,;1s        
#       :i,..,.              .,:,,::,.          .......... .......;1,       
#      ir,....:rrssr;:,       ,,.,::.     .r5S9989398G95hr;. ....,.:s,      
#     ;r,..,s9855513XHAG3i   .,,,,,,,.  ,S931,.,,.;s;s&BHHA8s.,..,..:r:     
#    :r;..rGGh,  :SAG;;G@BS:.,,,,,,,,,.r83:      hHH1sXMBHHHM3..,,,,.ir.    
#   ,si,.1GS,   sBMAAX&MBMB5,,,,,,:,,.:&8       3@HXHBMBHBBH#X,.,,,,,,rr    
#   ;1:,,SH:   .A@&&B#&8H#BS,,,,,,,,,.,5XS,     3@MHABM&59M#As..,,,,:,is,   
#  .rr,,,;9&1   hBHHBB&8AMGr,,,,,,,,,,,:h&&9s;   r9&BMHBHMB9:  . .,,,,;ri.  
#  :1:....:5&XSi;r8BMBHHA9r:,......,,,,:ii19GG88899XHHH&GSr.      ...,:rs.  
#  ;s.     .:sS8G8GG889hi.        ....,,:;:,.:irssrriii:,.        ...,,i1,  
#  ;1,         ..,....,,isssi;,        .,,.                      ....,.i1,  
#  ;h:               i9HHBMBBHAX9:         .                     ...,,,rs,  
#  ,1i..            :A#MBBBBMHB #  .r1,..        ,..;3BMBBBHBB#Bh.     ..                    ....,,,,,i1;   
#   :h;..       .,..;,1XBMMMMBXs,.,, .. :: ,.               ....,,,,,,ss.   
#    ih: ..    .;;;, ;;:s58A3i,..    ,. ,.:,,.             ...,,,,,:,s1,    
#    .s1,....   .,;sh,  ,iSAXs;.    ,.  ,,.i85            ...,,,,,,:i1;     
#     .rh: ...     rXG9XBBM#M#MHAX3hss13&&HHXr         .....,,,,,,,ih;      
#      .s5: .....    i598X&&A&AAAAAA&XG851r:       ........,,,,:,,sh;       
#      . ihr, ...  .         ..                    ........,,,,,;11:.       
#         ,s1i. ...  ..,,,..,,,.,,.,,.,..       ........,,.,,.;s5i.         
#          .:s1r,......................       ..............;shs,           
#          . .:shr:.  ....                 ..............,ishs.             
#              .,issr;,... ...........................,is1s;.               
#                 .,is1si;:,....................,:;ir1sr;,                  
#                    ..:isssssrrii;::::::;;iirsssssr;:..                    
#                         .,::iiirsssssssssrri;;:.
*/
package com.itheima.health.service.impl;

import com.itheima.health.dao.MemberMapper;
import com.itheima.health.dao.OrderMapper;
import com.itheima.health.dao.SetmealMapper;
import com.itheima.health.pojo.Order;
import com.itheima.health.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <dl>
 * <dd>描述: ~ ReportServiceImpl</dd>
 * <dd>创建时间：  10:59 2021/3/8</dd>
 * <dd>创建人： guodong</dd>
 * <dt>版本历史: </dt>
 * <pre>
 * Date         Author         Version     Description
 * ------------------------------------------------------------------
 * 2021/3/8      guodong         1.0        1.0 Version
 * </pre>
 * </dl>
 */
@DubboService
@Slf4j
public class ReportServiceImpl implements ReportService {

    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private OrderMapper orderMapper;

    /**
     * @Author: guodong
     * @Date: 10:52 2021/3/8
     * @ClassName: ReportController
     * @Parms []
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 获取会员统计数据
     */
    @Override
    public List<Integer> getMemberReport(List<String> months) {
        List<Integer> memberCount = new ArrayList<>();
        for (String month : months) {
            Integer count = memberMapper.findByMonth(month + ".31");
            memberCount.add(count);
        }
        return memberCount;
    }

    /**
     * @Author: guodong
     * @Date: 8:58 2021/3/9
     * @ClassName: ReportController
     * @Parms []
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 查询套餐统计数据
     */
    @Override
    public Map<String, Object> getSetmealReport() {
        Map<String, Object> resultMap = new HashMap<>();
        List<Map<String, Object>> setmealCount = setmealMapper.getCountSetmeal();
        resultMap.put("setmealCount", setmealCount);
        List<String> setmealNames = new ArrayList<>();
        //遍历查询结果中的套餐名称
        for (Map<String, Object> map : setmealCount) {
            String name = (String) map.get("name");
            setmealNames.add(name);
        }
        resultMap.put("setmealNames", setmealNames);
        return resultMap;
    }

    /**
     * @Author: guodong
     * @Date: 9:42 2021/3/9
     * @ClassName: ReportController
     * @Parms []
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 会员统计
     */
    @Override
    public Map<String, Object> getBusinessReportData() {
        Map<String, Object> resultMap = new HashMap<>();
        Calendar today = Calendar.getInstance();
        /*
            将时分秒毫秒清零
         */
        today.set(Calendar.HOUR_OF_DAY, 0);
        today.set(Calendar.MINUTE, 0);
        today.set(Calendar.SECOND, 0);
        today.set(Calendar.MILLISECOND, 0);
        //将周一设置为本周的第一天
        today.setFirstDayOfWeek(Calendar.MONDAY);

        //本周第一天
        Calendar weekFirstDay = (Calendar) today.clone();
        weekFirstDay.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        //本周最后一天
        Calendar weekLastDay = (Calendar) today.clone();
        weekLastDay.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        //本月第一天
        Calendar monthFirstDay = (Calendar) today.clone();
        monthFirstDay.set(Calendar.DAY_OF_MONTH, 1);
        //本月最后一天
        Calendar monthLastDay = (Calendar) today.clone();
        int actualMaximum = monthLastDay.getActualMaximum(Calendar.DAY_OF_MONTH);
        monthLastDay.set(Calendar.DAY_OF_MONTH, actualMaximum);

        //日期格式化
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //当天
        String todayStr = sdf.format(today.getTime());
        String weekFirstDayStr = sdf.format(weekFirstDay.getTime());
        String weekLastDayStr = sdf.format(weekLastDay.getTime());
        String monthFirstDayStr = sdf.format(monthFirstDay.getTime());
        String monthLastDayStr = sdf.format(monthLastDay.getTime());

        //返回结果数据
        resultMap.put("reportDate", todayStr);
        resultMap.put("totalMember", memberMapper.countTotal());
        resultMap.put("todayNewMember", memberMapper.countRangRegTime(todayStr, todayStr));
        resultMap.put("thisWeekNewMember", memberMapper.countRangRegTime(weekFirstDayStr, weekLastDayStr));
        resultMap.put("thisMonthNewMember", memberMapper.countRangRegTime(monthFirstDayStr, monthLastDayStr));

        resultMap.put("todayOrderNumber", orderMapper.countByOrderDateAndOrderStatus(todayStr, todayStr, null));
        resultMap.put("todayVisitsNumber", orderMapper.countByOrderDateAndOrderStatus(todayStr, todayStr, Order.ORDERSTATUS_YES));

        resultMap.put("thisWeekOrderNumber", orderMapper.countByOrderDateAndOrderStatus(weekFirstDayStr, weekLastDayStr, null));
        resultMap.put("thisWeekVisitsNumber", orderMapper.countByOrderDateAndOrderStatus(weekFirstDayStr, weekLastDayStr, Order.ORDERSTATUS_YES));

        resultMap.put("thisMonthOrderNumber", orderMapper.countByOrderDateAndOrderStatus(monthFirstDayStr, monthLastDayStr, null));
        resultMap.put("thisMonthVisitsNumber", orderMapper.countByOrderDateAndOrderStatus(monthFirstDayStr, monthLastDayStr, Order.ORDERSTATUS_YES));
        resultMap.put("hotSetmeal", setmealMapper.findHotSetmeal());
        return resultMap;
    }

    /**
     * @Author: Lenovo
     * @Date: 10:32 2021-3-11
     * @Parms []
     * @ReturnType: java.util.Map<java.lang.String   ,   java.lang.Object>
     * @Description: 会员统计
     */
    @Override
    public List<Integer> getMemberDate(List<String> months) {
        System.out.println(123);
        List<Integer> memberCount = new ArrayList<>();
        for (String month : months) {
            Integer count = memberMapper.findByMonth(month + ".31");
            memberCount.add(count);
        }
        return memberCount;

    }

    /**
     * @Author: 11341
     * @Date: 9:24 2021/3/13
     * @Parms []
     * @ReturnType: java.util.Map<java.lang.String,java.lang.Object>
     * @Description: 会员性别查询对比
    */
    @Override
    public Map<String, Object> getMemberSex() {
        Map<String, Object> resultMap = new HashMap<>();
        Integer memberMen = memberMapper.getMemberMen();
        Integer memberWomen = memberMapper.getMemberWomen();
        HashMap<String, Object> manMap = new HashMap<>();
        manMap.put("name", "男");
        manMap.put("value", memberMen);
        HashMap<String, Object> womanMap = new HashMap<>();
        womanMap.put("name", "女");
        womanMap.put("value", memberWomen);
        List<Map> list = new ArrayList<>();
        list.add(manMap);
        list.add(womanMap);
        resultMap.put("memberSex", list);
        return resultMap;
    }

    /**
     * @Author: 11341
     * @Date: 9:24 2021/3/13
     * @Parms []
     * @ReturnType: java.util.Map<java.lang.String,java.lang.Object>
     * @Description: 会员年龄查询对比
    */
    @Override
    public Map<String, Object> getMemberAge() {
        HashMap<String, Object> hashMap = new HashMap<>();

        Calendar today = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //45岁
        Calendar clone1 = (Calendar) today.clone();
        clone1.add(Calendar.YEAR, -45);
        //30岁
        Calendar clone2 = (Calendar) today.clone();
        clone2.add(Calendar.YEAR, -30);
        //18岁
        Calendar clone3 = (Calendar) today.clone();
        clone3.add(Calendar.YEAR, -18);


        String todayStr = sdf.format(today.getTime());
        String format1 = sdf.format(clone1.getTime());
        String format2 = sdf.format(clone2.getTime());
        String format3 = sdf.format(clone3.getTime());
        //45以上
        Integer memberAge1 = memberMapper.getMemberAge("0000-01-01", format1);
        //30到45
        Integer memberAge2 = memberMapper.getMemberAge(format1, format2);
        //18到30
        Integer memberAge3 = memberMapper.getMemberAge(format2, format3);
        //18以下
        Integer memberAge4 = memberMapper.getMemberAge(format3, todayStr);


        HashMap<String, Object> map1 = new HashMap<>();
        map1.put("name", "45以上");
        map1.put("value",memberAge1);
        HashMap<String, Object> map2 = new HashMap<>();
        map2.put("name", "30到45");
        map2.put("value",memberAge2);
        HashMap<String, Object> map3 = new HashMap<>();
        map3.put("name", "18到30");
        map3.put("value",memberAge3);
        HashMap<String, Object> map4 = new HashMap<>();
        map4.put("name", "18以下");
        map4.put("value",memberAge4);
        List<Map> memberCount = new ArrayList<>();
        memberCount.add(map1);
        memberCount.add(map2);
        memberCount.add(map3);
        memberCount.add(map4);
        hashMap.put("memberCount", memberCount);
        List<String> memberAges = new ArrayList<>();
        for (Map map : memberCount) {
            String name = (String) map.get("name");
            memberAges.add(name);
        }
        hashMap.put("memberAges", memberAges);
        return hashMap;
    }
}